﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml;

namespace CardsSimulator
{
    class CardGame
    {
        public static CardGame currentGame;

        public int currentPlayerId { get; set; }
        public int NumberOfPlayers { get; set; }
        public string Name { get; set; }

        public Component DeckComponent { get; set; }
        
        public List<Component> Desk { get; set; }
        public List<Player> Players { get; set; }

        public void AcceptMove(GameState move)
        {
            Players = move.Players;
            Desk = move.Desk;
        }

        public CardGame(string path)
        {
            Desk = new List<Component>();
            Players = new List<Player>();
            DeckComponent = new Component(ComponentType.Deck, "Talia");
            currentPlayerId = 0;

            #region Wczytywanie szablonu gry z pliku

            var document = new XmlDocument();
            document.Load(path);

            Debug.Assert(document.DocumentElement != null, "document.DocumentElement != null");
            Name = document.DocumentElement.Attributes["Nazwa"].InnerText;
            NumberOfPlayers = int.Parse(document.DocumentElement.Attributes["LiczbaGraczy"].InnerText);

            for (int i = 0; i < NumberOfPlayers; i++)
            {
                Players.Add(new Player());
            }


            XmlNode node = document.DocumentElement.SelectSingleNode("Talia");
            Debug.Assert(node != null, "node != null");
            foreach (XmlNode cardNode in node.ChildNodes)
            {
                Debug.Assert(cardNode.Attributes != null, "cardNode.Attributes != null");
                int x = int.Parse(cardNode.Attributes["Ilosc"].InnerText);
                for (int i = 0; i < x; i++)
                {
                    var value = (Value) Enum.Parse(typeof (Value), cardNode.Attributes["Wartosc"].InnerText);
                    var color = (Color) Enum.Parse(typeof (Color), cardNode.Attributes["Kolor"].InnerText);
                    DeckComponent.Cards.Add(new Card(color, value));
                }
            }

            node = document.DocumentElement.SelectSingleNode("Stol");
            foreach (XmlNode componentNode in node.ChildNodes)
            {
                var type = (ComponentType) Enum.Parse(typeof (ComponentType), componentNode.Attributes["Typ"].InnerText);
                var name = componentNode.Attributes["Nazwa"].InnerText;
                var numberOfCards = int.Parse(componentNode.Attributes["PoczatkowaLiczbaKart"].InnerText);
                var component = new Component(type, name, numberOfCards);

                Desk.Add(component);
            }

            node = document.DocumentElement.SelectSingleNode("Gracz");
            foreach (XmlNode componentNode in node.ChildNodes)
            {
                var type = (ComponentType) Enum.Parse(typeof (ComponentType), componentNode.Attributes["Typ"].InnerText);
                var name = componentNode.Attributes["Nazwa"].InnerText;
                var numberOfCards = int.Parse(componentNode.Attributes["PoczatkowaLiczbaKart"].InnerText);
                var component = new Component(type, name, numberOfCards);

                foreach (Player player in Players)
                {
                    player.Components.Add(component);
                }
            }

            #endregion

            #region rozdawanie kart

            DeckComponent.Shuffle();
            foreach (var player in Players)
            {
                foreach (var component in player.Components)
                {
                    for (int i = 0; i < component.InitialNumberOfCards; i++)
                    {
                        component.Cards.Add(DeckComponent.Cards.First());
                        DeckComponent.Cards.Remove(DeckComponent.Cards.First());
                    }
                }
            }

            foreach (var component in Desk)
            {
                for (int i = 0; i < component.InitialNumberOfCards; i++)
                {
                    component.Cards.Add(DeckComponent.Cards.First());
                    DeckComponent.Cards.Remove(DeckComponent.Cards.First());
                }
            }

            #endregion


        }
        public CardGame()
        {
            
        }
    }
}
